<?php
/**
 * Created by PhpStorm.
 * User: syzhou
 * Date: 2019-08-29
 * Time: 19:05
 */

namespace App\Http\Controllers;


use App\Project;
use App\Services\LargeGroupService;
use App\Services\ManageLargeGroupService;
use App\Services\ProjectService;
use App\Services\WorkflowService;
use App\WorkflowPhase;
use Illuminate\Support\Facades\Request;

class CommitteeAPIController extends Controller
{
    private $workflowSvc;

    public function __construct()
    {
        $this->middleware('auth');
        $this->workflowSvc = new WorkflowService();
    }

    public function getManageLargeGroupExpertReviewData()
    {
        $largeGroupId = Request::post('large_group_id');
        if (!is_numeric($largeGroupId)) {
            $user = Request::user();
            if (!$user->isLargeGroupLeader()) {
                return [
                    'ok' => false,
                    'code' => 3,
                    'msg' => '只有大组长能执行该操作',
                ];
            }
            $largeGroupId = $user->leading_large_group_id;
        }

        $result = (new ProjectService())->leaderGetExpertReviewProjects($largeGroupId);
        if (!$result['ok']) {
            return $this->json($result);
        }

        $result2 = (new ManageLargeGroupService())->getLargeGroupById($largeGroupId);
        if (!$result2['ok']) {
            return $this->json($result2);
        }
        $result['large_group'] = $result2['large_group'];
        return $this->json($result);
    }

    public function leaderReviewExpertReviewResult() {
        $projectId = Request::get('projectId');
        $reviewResult = Request::get('reviewResult');
        $reviewRemark = Request::get('reviewRemark');
        $result = (new ManageLargeGroupService())->leaderReviewExpertReviewResult($projectId, $reviewResult, $reviewRemark);
        return $this->json($result);
    }

    public function submitExpertReviewResult()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitExpertReviewResult($largeGroupId);
        return $this->json($result);
    }

    public function getManageLargeGroupMeetingReviewData()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $result = (new ProjectService())->leaderGetMeetingReviewProjects($largeGroupId);
        return $this->json($result);
    }

    /**
     * 提交会评
     * @return array|\Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/8/11 7:35 下午
     * @流程:
     */
    public function leaderSubmitMeetingReviewResult() {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitMeetingReviewResult($largeGroupId);
        return $this->json($result);
    }

    public function getManageLargeGroup2Data()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->getManageLargeGroup2Data($largeGroupId);
        return $this->json($result);
    }

    public function setUndeterminedInterval()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $undeterminedIntervalBegin = Request::get('undeterminedIntervalBegin');
        $undeterminedIntervalEnd = Request::get('undeterminedIntervalEnd');
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSetUndeterminedInterval($largeGroupId, $undeterminedIntervalBegin, $undeterminedIntervalEnd);
        return $this->json($result);
    }

    public function submitSetUndeterminedIntervalResult()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitPlanAward2Result($largeGroupId);
        return $this->json($result);
    }

    public function getManageLargeGroup3Data()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->getManageLargeGroup3Data($largeGroupId);
        return $this->json($result);
    }

    public function getManageLargeGroupVoteData()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->getManageLargeGroupVoteData($largeGroupId);
        return $this->json($result);
    }

    public function getManageLargeGroup5Data()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->getManageLargeGroup5Data($largeGroupId);
        return $this->json($result);
    }

    public function submitVote1Result()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitVote1Result($largeGroupId);
        return $this->json($result);
    }

    public function submitVoteResult()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitVoteResult($largeGroupId);
        return $this->json($result);
    }

    public function updateFinalAwardLevel() {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $data = Request::all();
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderUpdateFinalAwardLevel($largeGroupId, $data['project_id'], $data['final_award_level']);
        return $this->json($result);
    }

    public function submitMeetingReviewResult() {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitMeetingReviewResult($largeGroupId);
        return $this->json($result);
    }

    public function getVote1Data()
    {
        $user = Request::user();
        if (!$user->isCommittee()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有奖励委委员能执行该操作',
            ];
        }
        $largeGroupId = Request::get('large_group_id');
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->committeeGetVote1Data($user->id, $largeGroupId);
        return $this->json($result);
    }

    public function getVoteData()
    {
        $user = Request::user();
        if (!$user->isCommittee()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有奖励委委员能执行该操作',
            ];
        }
        $largeGroupId = Request::get('large_group_id');
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->committeeGetVoteData($user->id, $largeGroupId);
        return $this->json($result);
    }

    public function vote1()
    {
        $user = Request::user();
        if (!$user->isCommittee()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有奖励委委员能执行该操作',
            ];
        }
        $data = Request::all();
        $data['commissioner_id'] = $user->id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->committeeVote1($data);
        return $this->json($result);
    }

    public function vote()
    {
        $user = Request::user();
        if (!$user->isCommittee()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有奖励委委员能执行该操作',
            ];
        }
        $data = Request::all();
        $data['commissioner_id'] = $user->id;
        $result = (new ProjectService())->committeeVote($data);
        return $this->json($result);
    }

    public function voteBatchAgree() {
        $user = Request::user();
        if (!$user->isCommittee()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有奖励委委员能执行该操作',
            ];
        }
        $data = Request::all();
        $data['commissioner_id'] = $user->id;
        $result = (new ProjectService())->voteBatchAgree($data);
        return $this->json($result);
    }

    public function getManageLargeGroupDefenseData()
    {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->getManageLargeGroupDefenseData($largeGroupId);
        return $this->json($result);
    }

    public function  updateDefenseAwardLevel() {
        $projectId = Request::get('project_id');
        $defenseAwardLevel = Request::get('defense_award_level');
        $result = (new ManageLargeGroupService())->updateDefenseAwardLevel($projectId, $defenseAwardLevel);
        return $this->json($result);
    }

    public function leaderSubmitDefenseResult() {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $manageLargeGroupService = new ManageLargeGroupService();
        $result = $manageLargeGroupService->leaderSubmitDefenseResult($largeGroupId);
        return $this->json($result);
    }


    /**
     * 会评初选后（按照比例设定入选会评），个别项目调整（加入会评）
     * @return array|\Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/10 3:27 下午
     * @流程:
     */
    public function addOrRemoveMeeting() {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }

        $largeGroupId = $user->leading_large_group_id;
        $project_id = (int) Request::post('project_id');
        $remark = (string) Request::post('remark');
        $isAdd  = (bool)  Request::post('is_add');
        $status = $isAdd ? Project::STATUS_MEETING_REVIEW_TO_REVIEW : Project::STATUS_EXPERT_REVIEW_REJECTED;
        $result = (new ManageLargeGroupService())->AddOrRemoveMeetingProject($project_id,$largeGroupId,$remark,$status);
        return $this->json($result);
    }

    /**
     * 会评初选 按照比例设定入选会评
     * @return array|\Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/10 3:26 下午
     * @流程:
     */
    public function setMeetingProjectNumber() {
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $meetingNum = (int) Request::post('meetingNum');
        $result = (new ManageLargeGroupService())->SetMeetingProjectNumber($largeGroupId,$meetingNum);
        return $this->json($result);
    }


    /**
     * 设置项目会评排名
     * @return array|\Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/17 3:00 下午
     * @流程:
     */
    public function setMeetingRank(){
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $project_id = (int) Request::post('project_id');
        $rank = (int) Request::post('rank');
        $largeGroupId = $user->leading_large_group_id;
        $result = (new ManageLargeGroupService())->SetMeetingRank($project_id,$rank,$largeGroupId);
        return $this->json($result);
    }


    /**
     * 配置大组奖项
     * @return array|\Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/17 6:17 下午
     * @流程:
     */
    public function setAwardsConfigure(){
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) {
            return [
                'ok' => false,
                'code' => 3,
                'msg' => '只有大组长能执行该操作',
            ];
        }
        $largeGroupId = $user->leading_large_group_id;
        $awardsConfigure = Request::post('configure');
        $result = (new ManageLargeGroupService())->SetAwardsConfigure($largeGroupId,$awardsConfigure);
        return $this->json($result);
    }

}